package org.zerg.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zerg.dao.OrderDao;
import org.zerg.dao.ProductDao;
import org.zerg.dto.Report;
import org.zerg.entity.Order;
import org.zerg.entity.OrderProducts;
import org.zerg.entity.Product;
import org.zerg.service.ReportService;

@Service
public class ReportServiceImpl implements ReportService{
	
	@Autowired
	private ProductDao productDao;
	
	@Autowired
	private OrderDao orderDao;
	/*
	 * 1.先从商品订单中取出分类名称（作为纵坐标）
	 * 2.再对每样商品化为类型*商品数量
	 * 3.返回list
	 */
	@Override
	public Report reportByCate() {
		Report report = new Report();
		List<OrderProducts> posductList = orderDao.getOrderProductAll();
		//统计类名-----纵坐标
		List<String> nameList = this.getNameList(posductList);
		//统计数据集
		List<Integer> dateList = this.getDateList(posductList,nameList);
		report.setxAxis(nameList);
		report.setSeries(dateList);
		return report;
	}
	
	//统计数据集
	private List<Integer> getDateList(List<OrderProducts> posductList,List<String> nameList) {
		List<Integer> list = new ArrayList<Integer>();
		for (String nameCate : nameList) {
			Integer number = 0;
			for (OrderProducts pr : posductList) {
				Product product = productDao.getProductById(pr.getProductId());
				if(nameCate!=null){
					if(product.getCategory().equals(nameCate)){
						number = number + (1*pr.getCounts());
					}	
				}
			}
			list.add(number);
		}
		return list;
	}
	
	//统计类名-----纵坐标
	private List<String> getNameList(List<OrderProducts> posductList) {
		List<String> list = new ArrayList<String>();
		for (OrderProducts orderProducts : posductList) {
			Product product = productDao.getProductById(orderProducts.getProductId());
			if(repeatCate(product.getCategory(),list)){
				list.add(product.getCategory());
			}
		}
		return list;
	}

	private boolean repeatCate(String category, List<String> list) {
		boolean result = true;
		for (String string : list) {
			if(string!=null){
				if(category.equals(string)){
					result = false;
				}
			}
		}
		return result;
	}

	//根据天--统计销售情况
	@Override
	public Report reportByDay() {
		Report report = new Report();
		List<Order> list = orderDao.getOrderAll();
		//统计日期-----纵坐标
		List<String> dayList = this.getDayList(list);
		//统计销售（价格）
		List<Double> priceList = this.getPriceList(list,dayList);
		report.setxAxis(dayList);
		report.setSeries2(priceList);
		return report;
	}

	private List<String> getDayList(List<Order> orderList) {
		List<String> list = new ArrayList<String>();
		for (Order order : orderList) {
			String time = order.getCreateTime();
			int first = time.indexOf("年");
			int last = time.indexOf("日");
			String useTime = time.substring(first+1, last+1);
			if(repeatCate(useTime, list)){
				list.add(useTime);
			}
		}
		return list;
	}

	private List<Double> getPriceList(List<Order> orderList,
			List<String> dayList) {
		List<Double> list = new ArrayList<Double>();
		//对照日期顺序输出日销售量
		for (String day : dayList) {
			Double number = 0.0;	
			//循环订单--找出日期相同的订单
			for (Order order : orderList) {
				String time = order.getCreateTime();
				int first = time.indexOf("年");
				int last = time.indexOf("日");
				String useTime = time.substring(first+1, last+1);
				if(day!=null){
					if(day.equals(useTime)){
						//循环订单的商品单----计算总价格
						List<OrderProducts> posductList = orderDao.getProductByOrder(order.getOrderNo());
						for (OrderProducts orderProducts : posductList) {
							Product product = productDao.getProductById(orderProducts.getProductId());
							number = number + (product.getPrice()*orderProducts.getCounts());
						}
					}
				}
			}
			list.add(number);
		}
		return list;
	}

}
